<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
    >
      <el-form-item label="开始日期">
        <el-date-picker
          type="date"
          placeholder="请选择开始时间"
          v-model="queryParams.beginClassDate"
          value-format="yyyy-MM-dd"
        />
      </el-form-item>
      <el-form-item label="结束时间">
        <el-date-picker
          type="date"
          placeholder="请选择结束时间"
          v-model="queryParams.endClassDate"
          value-format="yyyy-MM-dd"
        />
      </el-form-item>

      <el-form-item label="合同编号">
        <el-input
          v-model="queryParams.contractCode"
          placeholder="请输入合同编号"
          clearable
        />
      </el-form-item>
      <el-form-item label="学员姓名">
        <el-input
          v-model="queryParams.customerName"
          placeholder="请输入学员姓名"
          clearable
        />
      </el-form-item>
      <el-form-item label="学员编号">
        <el-input
          v-model="queryParams.customerCode"
          placeholder="请输入学员编号"
          clearable
        />
      </el-form-item>

      <el-form-item label="教师">
        <userSelect v-model="queryParams.teacherId" placeholder="请选择教师" />
      </el-form-item>
      <el-form-item label="科目">
        <el-select v-model="queryParams.subjectId" placeholder="请选择科目">
          <el-option
            v-for="item in classOptions"
            :key="item.dictCode"
            :label="item.dictLabel"
            :value="item.dictCode"
            :disabled="item.status == 1"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="所属校区">
        <campusSelect v-model="queryParams.campusId" />
      </el-form-item>
      <el-form-item label="年级">
        <el-select
          v-model="queryParams.grade"
          placeholder="请选择年级"
          clearable
        >
          <el-option
            v-for="item in gradeOpiton"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="contractList">
      <el-table-column label="序号" type="index" align="center" />
      <el-table-column
        label="学员"
        align="center"
        prop="customerName"
        min-width="100"
      />
      <el-table-column label="教师" align="center" prop="teacherName" />
      <el-table-column label="校区" align="center" prop="campusName" />
      <el-table-column label="年级" align="center" prop="grade">
        <template #default="{ row }">
          <span>{{ formatGrade(row.grade) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="科目" align="center" prop="subjectName" />
      <el-table-column label="日期" align="center" prop="classDate" />
      <el-table-column label="时间段" align="center" prop="classTimeSlot" />
      <el-table-column label="时长" align="center" prop="classHour" />
      <el-table-column label="课时人数" align="center" prop="peopleNum" />
      <el-table-column label="确认时间" align="center" prop="confirmDate" />
      <el-table-column label="确认者" align="center" prop="confirmper" />

      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-search"
            @click="getDetail(scope.row)"
          >
            详情</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { confirmList } from "@/api/course/list";
import campusSelect from "@/components/Select/campusSelect.vue";
import userSelect from "@/components/Select/userSelect.vue";
export default {
  name: "Contract",
  components: {
    campusSelect,
    userSelect,
  },
  data() {
    return {
      // 遮罩层
      loading: true,

      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 合同表格数据
      contractList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },

      // 年级选项
      gradeOpiton: [],
      classOptions: [],
    };
  },
  created() {
    this.gradeOpiton = [
      { value: 0, label: "小一" },
      { value: 1, label: "小二" },
      { value: 2, label: "小三" },
      { value: 3, label: "小四" },
      { value: 4, label: "小五" },
      { value: 5, label: "小六" },
      { value: 6, label: "初一" },
      { value: 7, label: "初二" },
      { value: 8, label: "初三" },
      { value: 9, label: "高一" },
      { value: 10, label: "高二" },
      { value: 11, label: "高三" },
      { value: 12, label: "高四" },
    ];

    this.getList();
    // 获取合同列表

    this.getClassOptions();
  },
  methods: {
    /** 查询合同列表 */
    getList() {
      this.loading = true;
      confirmList(this.queryParams).then((response) => {
        this.contractList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    /** 查询科目选项 */
    getClassOptions() {
      this.getDicts("sys_subject_class").then((response) => {
        this.classOptions = response.data;
        console.log(this.classOptions);
      });
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    formatGrade(grade) {
      return _.find(this.gradeOpiton, { value: grade })?.label || "未知年级";
    },

    formatStudent(students) {
      return _.map(students, "customerName").join(", ");
    },
  },
};
</script>
<style lang="scss" scoped>
.form {
  &-input {
    width: 400px;
  }
  &-select {
    width: 400px;
  }

  &-date-picker {
    width: 400px;
  }
}
</style>
